/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.meteoCal.control;

import it.polimi.meteoCal.entity.Event;
import it.polimi.meteoCal.entity.Forecast;
import it.polimi.meteoCal.entity.InvitedList;
import it.polimi.meteoCal.entity.InvitedListPK;
import it.polimi.meteoCal.entity.Message;
import it.polimi.meteoCal.entity.MessageList;
import it.polimi.meteoCal.entity.MessageListPK;
import it.polimi.meteoCal.entity.User;
import javax.ejb.Stateless;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author paolo
 */
@Stateless
@Named
public class TestDB {

     @PersistenceContext(unitName = "meteoCalPU")
     private EntityManager em;
    
    public void test(){
       Event event = new Event();
       Forecast forecast = new Forecast();
       InvitedList invitedList = new InvitedList();
       Message message = new Message();
       MessageList messageList = new MessageList();
       User user = new User();
       //create user "mario"
       user.setEmail("mario@mario.it");
       user.setPassword("asd");
       user.setName("mario");
       user.setSurname("rossi");
       user.setPublicCalendar(Boolean.TRUE);
       user.setGroupname("user");
       em.persist(user);
       //create event gita a milano of mario
       event.setAuthor(user);
       event.setName("gita a milano");
       event.setStartDate(null);
       event.setStartHour(null);
       event.setEndDate(null);
       event.setEndHour(null);
       event.setCity("Milano");
       event.setCountry("IT");
       event.setDeleted(Boolean.FALSE);
       event.setOutdoor(Boolean.TRUE);
       event.setPublicEvent(Boolean.TRUE);
       em.persist(event);
       //create forecast for event in milan of mario and update event with the forecast
       forecast.setCity("Milano");
       forecast.setCountry("IT");
       forecast.setForecastDate(null);
       forecast.setUpdateDate(null);
       forecast.setBadCondition(Boolean.TRUE);
       em.persist(forecast);
       event.setForecast(forecast);
       em.merge(event);
       //create user "luigi"
       User user1 = new User(); 
       user1.setEmail("luigi@mario.it");
       user1.setPassword("asd");
       user1.setName("luigi");
       user1.setSurname("bianchi");
       user1.setPublicCalendar(Boolean.FALSE);
       user1.setGroupname("user");
       em.persist(user1);
       //mario invita luigi all'evento "gita a milano"
       InvitedListPK pkil = new InvitedListPK();
       pkil.setEvent(event.getEventID());
       pkil.setUser(user1.getEmail());
       invitedList.setInvitedListMP(pkil);
       em.persist(invitedList);
       message.setEvent(event);
       message.setType(1);
       em.persist(message);
       MessageListPK pkml = new MessageListPK();
       pkml.setMessage(message.getMessageID());
       pkml.setUser(user1.getEmail());
       messageList.setMessageListPK(pkml);
       messageList.setPending(Boolean.TRUE);
       em.persist(messageList);
       //print all event in wich mario is an author
       User mario = em.find(User.class, "mario@mario.it");
        for (Event e : mario.getEventCollection()) {
            System.out.println(e.getAuthor()+"---"+e.getName()); 
        }
        //print all event in wich luigi is invited
        TypedQuery<InvitedList> query = em.createQuery("SELECT i FROM InvitedList i, User u WHERE i.user=u AND u.email='luigi@mario.it'", InvitedList.class);
        java.util.List<InvitedList> resultList = query.getResultList();
        for (InvitedList i : resultList) {
            System.out.println(i.getEvent().getName()+"--"+i.getUser().getEmail() +"  sei stato invitato da" + i.getEvent().getAuthor().getEmail());
        }
    }
}
